﻿using Microsoft.AspNetCore.Mvc;
using 订单处理系统.Entity;
using 订单处理系统.IService.impl;
using 订单处理系统.Model;

namespace 订单处理系统.Controllers
{
    public class ApiController : BaseContoller
    {
        private readonly RabbitMQService _rabbitmqService;
        private readonly IRedisService _redisService;
        private readonly OrderDbContext _db;

        public ApiController(RabbitMQService rabbitmqService, IRedisService redisService, OrderDbContext orderDbContext)
        {
            _rabbitmqService = rabbitmqService;
            _redisService = redisService;
            _db = orderDbContext;
        }

        [HttpPost]
        public async Task<string?> submitOrder(Order order)
        {
            //var limitKey = $"limit:user:{order.userId}";
            //if (await _redisService.keyExitstsAsync(limitKey)) return "操作太频繁";

            //await _redisService.SetStringAsync(limitKey, "1", TimeSpan.FromSeconds(1));
            long count = await _redisService.DecrementAsync("produc:2");
            if (count < 0)
            {
                await _redisService.IncrementAsync("produc:2");
                return "库存已清空";
            }
            await _redisService.DecrementAsync("produc:2");
            await _rabbitmqService.SendAsync("", "order:create", order);
            return "已下单";
        }
    }
}